<template>
	<el-dialog :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item prop="platform" :label="$t('AppVersion.platform')">
						<el-radio-group v-model="form.platform" class="ml-4">
							<el-radio label="android" size="large">android</el-radio>
							<el-radio label="ios" size="large">ios</el-radio>
						</el-radio-group>
						<!--						<el-input v-model="form.platform" :placeholder="$t('AppVersion.inputPlatformTip')" clearable show-word-limit :maxlength="128" />-->
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="appId" :label="$t('AppVersion.appId')">
						<el-input v-model="form.appId" :placeholder="$t('AppVersion.inputAppIdTip')" clearable show-word-limit :maxlength="128" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="editionName" :label="$t('AppVersion.editionName')">
						<el-input v-model="form.editionName" :placeholder="$t('AppVersion.inputEditionNameTip')" clearable show-word-limit :maxlength="128" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="editionNumber" :label="$t('AppVersion.editionNumber')">
						<el-input v-model="form.editionNumber" :placeholder="$t('AppVersion.inputEditionNumberTip')" clearable show-word-limit />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="editionUrl" :label="$t('AppVersion.editionUrl')">
						<el-input v-model="form.editionUrl" :placeholder="$t('AppVersion.inputEditionUrlTip')" clearable show-word-limit :maxlength="1024" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="editionForce" :label="$t('AppVersion.editionForce')">
						<el-radio-group v-model="form.editionForce">
							<el-radio :label="item.value" v-for="(item, index) in yes_no_type" border :key="index">{{ item.label }} </el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="packageType" :label="$t('AppVersion.packageType')">
						<el-radio-group v-model="form.packageType">
							<el-radio :label="0" border>{{ $t('AppVersion.WholePacketUpdate') }}</el-radio>
							<el-radio :label="1" border>{{ $t('AppVersion.wgtHotUpdate') }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="editionIssue" :label="$t('AppVersion.editionIssue')">
						<el-radio-group v-model="form.editionIssue">
							<el-radio :label="item.value" v-for="(item, index) in yes_no_type" border :key="index">{{ item.label }} </el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="editionSilence" :label="$t('AppVersion.editionSilence')">
						<el-radio-group v-model="form.editionSilence">
							<el-radio :label="item.value" v-for="(item, index) in yes_no_type" border :key="index">{{ item.label }} </el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="describe" :label="$t('AppVersion.describe')">
						<editor height="400" v-model:get-html="form.describe" />

						<!--            <el-input v-model="form.describe" :placeholder="$t('AppVersion.inputDescribeTip')" clearable show-word-limit :maxlength="65535" />-->
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item prop="remark" :label="$t('AppVersion.remark')">
						<el-input v-model="form.remark" :placeholder="$t('AppVersion.inputRemarkTip')" clearable show-word-limit :maxlength="255" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AppVersionDialog">
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/bizs/AppVersion';
const emit = defineEmits(['refresh']);

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 定义字典
import { useDict } from '/@/hooks/dict';
const { yes_no_type } = useDict('yes_no_type');

// 定义变量内容
const visible = ref(false);
const loading = ref(false);
const dataFormRef = ref();

// 提交表单数据
const form = reactive({
	id: '',
	platform: 'android', // 平台  android | ios
	appId: '', // appid
	editionName: '', // 版本名称
	editionNumber: '', // 版本号
	editionUrl: '', // 版本地址
	editionForce: '', // 是否强制更新
	packageType: '', // 包类型
	editionIssue: '', // 是否发行
	editionSilence: '', // 是否静默更新
	describe: '', // 版本描述
	remark: '', // 备注
});

// 定义校验规则
import { rule } from '/@/utils/validate';
import Editor from '/@/components/Editor/index.vue';
const dataRules = ref({
	platform: [{ required: true, message: t('AppVersion.inputPlatformTip'), trigger: 'blur' }],
	appId: [{ required: false, message: t('AppVersion.inputAppIdTip'), trigger: 'blur' }],
	editionName: [{ required: true, message: t('AppVersion.inputEditionNameTip'), trigger: 'blur' }],
	editionNumber: [{ required: true, message: t('AppVersion.inputEditionNumberTip'), trigger: 'blur' }],
	editionUrl: [{ required: false, message: t('AppVersion.inputEditionUrlTip'), trigger: 'blur' }],
	editionForce: [{ required: true, message: t('AppVersion.inputEditionForceTip'), trigger: 'blur' }],
	packageType: [{ required: true, message: t('AppVersion.inputPackageTypeTip'), trigger: 'blur' }],
	editionIssue: [{ required: true, message: t('AppVersion.inputEditionIssueTip'), trigger: 'blur' }],
	editionSilence: [{ required: true, message: t('AppVersion.inputEditionSilenceTip'), trigger: 'blur' }],
	describe: [{ required: false, message: t('AppVersion.inputDescribeTip'), trigger: 'blur' }],
	remark: [{ required: false, message: t('AppVersion.inputRemarkTip'), trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
		// Object.assign(form, {
		// 	id: '',
		// 	platform: 'android',
		// 	appId: '',
		// 	editionName: '',
		// 	editionNumber: '',
		// 	editionUrl: '',
		// 	editionForce: '',
		// 	packageType: '',
		// 	editionIssue: '',
		// 	editionSilence: '',
		// 	describe: '',
		// 	remark: '',
		// });
	});

	// 获取信息
	if (id) {
		form.id = id;
		initFormData(id);
	}
};

// 初始化表单
const initFormData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

// 提交表单
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
